package com.qo.qsdk.ui.base.floater;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.net.http.SslError;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.qo.qsdk.utils.Language;


/**
 * Created by Anmi on 2018/8/31.
 * Describe:
 */
public class WebListenerUtil {

    private static WebListenerUtil instance;

    private WebChromeClient mWebChromeClient;
    private WebViewClient mWebViewClient;

    public static WebListenerUtil getInstance() {
        if (instance == null) {
            synchronized (WebListenerUtil.class) {
                if (instance == null) {
                    instance = new WebListenerUtil();
                }
            }
        }
        return instance;
    }

    public WebChromeClient getWebChromeClient(final View loadingView) {
        mWebChromeClient = new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    loadingView.setVisibility(View.GONE);
                } else {
                    if (loadingView.getVisibility() == View.GONE)
                        loadingView.setVisibility(View.VISIBLE);
                }
                super.onProgressChanged(view, newProgress);
            }

        };

        return mWebChromeClient;
    }

    public WebViewClient getWebViewClient(final Activity activity) {
        mWebViewClient = new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                if(url.startsWith("sms")){
                    Uri uri= Uri.parse(url);
                    Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
                    activity.startActivity(intent);
                }else {
                    view.loadUrl(url);
                }

                return true;
            }

            @Override
            public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
                try {
                    AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
                    builder.setMessage(Language.Float_Ssl_Error.toString());
                    builder.setPositiveButton(Language.Float_Ssl_Error_Enter.toString(), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            handler.proceed();// 接受https所有网站的证书
                        }
                    });

                    builder.setNegativeButton(Language.Float_Ssl_Error_Cancel.toString(), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            handler.cancel();
                        }
                    });

                    AlertDialog dialog = builder.create();
                    dialog.show();
                }catch (Exception e){
                    if (handler != null) handler.cancel();
                }
            }
        };
        return mWebViewClient;
    }
}
